package mission_tour.solution;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class tour_infomation_page extends Activity {
    /** Called when the activity is first created. */
	String phonenumber ="";
	AlertDialog.Builder nologin;
	LocationManager lm = null;
	ComponentName tService;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tour_infomation);
        
		stopGPSservice();
		
        Button go_main_btn = (Button) findViewById(R.id.go_main_btn);
        
        go_main_btn.setOnTouchListener(new View.OnTouchListener(){
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
					
				}else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
					startGPSservice();
					
					Intent intent = new Intent(tour_infomation_page.this, tour_main.class);
					startActivityForResult(intent, 0);
					finish();
					
				}
					return false;
			}
		});
    }
    
    private void startGPSservice(){
    	tService = startService(new Intent(this,background_event_service.class));
    }
    
    private void stopGPSservice(){
    	if(tService == null){
    		return;
    	}
    	
    	Intent intent = new Intent();
    	intent.setComponent(tService);
    	if(stopService(intent)){
    		System.out.println(tService.toShortString()+"stop ok");
    	}else{
    		System.out.println(tService.toShortString()+"stop her nonono");
    	}
    }
    public boolean onKeyDown( int KeyCode, KeyEvent event ){
		if( event.getAction() == KeyEvent.ACTION_DOWN ){
			
			if( KeyCode == KeyEvent.KEYCODE_BACK ){
				items.back_push++;
				long currentTime = System.currentTimeMillis();
				if(items.back_push == 1){
					items.touchTime = System.currentTimeMillis();
					Toast t =  Toast.makeText(tour_infomation_page.this,"종료 하시려면\n한번 더 뒤로가기 버튼을 눌러주세요",Toast.LENGTH_SHORT);
				    t.setDuration(2000);
				    t.show();
				          
				    new Thread(new Runnable() {    
						@Override
						public void run() {
							try {
								Thread.sleep(2000);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
							items.back_push=0;
							}
				    	}).start();
				}else if(items.back_push == 2){
					if(currentTime - items.touchTime <= 2000 ){
						finish();
						return super.onKeyDown(KeyCode, event);
					}					
				}
				return true;
			}
		}
		return super.onKeyDown(KeyCode, event);
	}
}
